﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SilkeyERP.Model.Repository
{
    public class CustomerRepository : ICustomerRepository
    {

        private bool disposed = false;
        private Model.SilkeyERPEntities context;

        public CustomerRepository()
        {
            if (this.context == null)
            this.context = new SilkeyERPEntities();            
        }

        public IEnumerable<Customer> GetAll()
        {
            return context.Customer.ToList();
        }

        public Customer GetByID(int ID)
        {
            return context.Customer.Find(ID);
        }

        public void Insert(Customer Obj)
        {
            context.Customer.Add(Obj);
            Save();
        }

        public void Delete(int ID)
        {
            Customer val = context.Customer.Find(ID);
            context.Customer.Remove(val);
            Save();
        }

        public void DeleteLogical(int ID)
        {
            Customer Obj = GetByID(ID);
            Obj.Enable = false;
            Update(Obj);
        }

        public void Update(Customer Obj)
        {
            context.Entry(Obj).State = EntityState.Modified;
            Save();
        }

        public void Save()
        {
            context.SaveChanges();
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
                if (disposing)
                    context.Dispose();
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

    }
}
